<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Internationalization Tests</title>

<script type="text/javascript" src="../../../build/yui/yui.js"></script>

<style type="text/css">
    #console .yui3-console-entry {
        padding:2px;
        margin:0px;
        min-height:0;
    }
    
    #console .yui3-console-entry-fail .yui3-console-entry-cat {
        background-color:red;
    }

    #console .yui3-console-entry-pass .yui3-console-entry-cat {
        background-color:green;
    }

    #console .yui3-console-entry-perf .yui3-console-entry-cat {
        background-color:blue;
    }

    #console {
        position:static;
    }

    html, body {
        height:100%;
    }
</style>
</head>

<body class="yui3-skin-sam">
<p><input type="button" value="Run Tests" id="btnRun" disabled=true></p>
<script type="text/javascript">

YUI({
    useBrowserConsole:false
}).use("console", "test", "intl", function(Y) {

    // Set up the page
    var LANG = Y.Lang,
        ASSERT = Y.Assert,
        ARRAYASSERT = Y.ArrayAssert,

        areObjectsReallyEqual = function (o1, o2) {
            Y.ObjectAssert.areEqual(o1, o2);
            Y.ObjectAssert.areEqual(o2, o1);
        };

    var testLookupBestLanguage = new Y.Test.Case({
        name: "Lookup Best Language Tests",
    
        testArray: function() {
            var language = Y.Intl.lookupBestLang(["de-DE", "en-SG", "ja-JP"], ["fr-FR", "en", "en-SG"]);
            ASSERT.areSame(language, "en-SG", "Expected en-SG.");
        },

        testString: function() {
            var language = Y.Intl.lookupBestLang("de-DE,en-SG,ja-JP", ["fr-FR", "en", "en-SG"]);
            ASSERT.areSame(language, "en-SG", "Expected en-SG.");
        },

        testStar: function() {
            var language = Y.Intl.lookupBestLang("de-DE,*,en-SG,ja-JP", ["fr-FR", "en", "en-SG"]);
            ASSERT.areSame(language, "en-SG", "Expected en-SG.");
        },

        testPrivate: function() {
            var language = Y.Intl.lookupBestLang("de-DE,zh-Hant-CN-x-private1-private2", ["fr-FR", "zh-Hant-CN", "zh-Hant-CN-x"]);
            ASSERT.areSame(language, "zh-Hant-CN", "Expected zh-Hant-CN.");
        }
    });
    
    var testStore = new Y.Test.Case({
        name: "Basic Intl Language Store Tests",

        testAdd: function() {

            var eventStack = [],
                expectedEventStack = [
                    {module:"modA", newVal:"fr", prevVal:"en-US"},
                    {module:"modA", newVal:"ja-JP", prevVal:"fr"},
                    {module:"modA", newVal:"fr", prevVal:"ja-JP"}
                ];

            Y.Intl.add("modA", "en-US", {
                hello:"Hello"
            });

            ASSERT.areSame(Y.Intl.get("modA").hello, "Hello");

            var h = Y.Intl.after("intl:langChange", function(e) {
                eventStack.push({module:e.module, newVal:e.newVal, prevVal:e.prevVal});
            });

            Y.Intl.add("modA", "fr", {
                hello:"Bonjour"
            });

            ASSERT.areSame(Y.Intl.get("modA").hello, "Bonjour");

            Y.Intl.add("modA", "ja-JP", {
                hello: "こんにちは"
            });

            ASSERT.areSame(Y.Intl.get("modA").hello, "こんにちは");

            Y.Intl.setLang("modA", "fr");
            ASSERT.areSame(Y.Intl.get("modA").hello, "Bonjour");

            ARRAYASSERT.itemsAreEquivalent(expectedEventStack, eventStack, function(a, b) {
                var equal = true;
                try {
                    areObjectsReallyEqual(a, b);
                } catch(e) {
                    equal = false;
                }
                return equal;
            });
            
            h.detach();
        },

        testRoot : function() {

            Y.Intl.add("modB", "fr", {
                hello:"Bonjour"
            });

            ASSERT.areSame(Y.Intl.getLang("modB"), "fr");

            Y.Intl.add("modB", "", {
               hello:"HELLO"
            });

            ASSERT.areSame(Y.Intl.getLang("modB"), "");
            ASSERT.areSame(Y.Intl.get("modB").hello, "HELLO");

            Y.Intl.setLang("modB", "fr");

            ASSERT.areSame(Y.Intl.get("modB").hello, "Bonjour");
            ASSERT.areSame(Y.Intl.getLang("modB"), "fr");
        },

        testGet : function() {

            var origSet = {
                hello : "Hello",
                world : "World"
            };

            Y.Intl.add("modC", "en", {
                hello : "Hello",
                world : "World"
            });

            var strs = Y.Intl.get("modC");
            areObjectsReallyEqual(strs, origSet);

            ASSERT.areSame(Y.Intl.get("modC", "hello"), origSet.hello);
            ASSERT.areSame(Y.Intl.get("modC", "world"), origSet.world);
        }
    });

    var suite = new Y.Test.Suite({name:"Intl Unit Tests"});

    suite.add(testLookupBestLanguage);
    suite.add(testStore);

    Y.Test.Runner.setName("Intl Tests");
    Y.Test.Runner.add(suite);
    Y.Test.Runner.disableLogging();
    Y.Test.Runner.run();

    var console;

    Y.one("#btnRun").set("disabled", false).on("click", function() {
        if (!console) {
            console = new Y.Console({
                id:"console",
                width:"100%",
                height:"90%",
                verbose : false,
                printTimeout: 0,
                newestOnTop : false,
                entryTemplate: '<pre class="{entry_class} {cat_class} {src_class}">'+
                        '<span class="{entry_cat_class}">{label}</span>'+
                        '<span class="{entry_content_class}">{message}</span>'+
                '</pre>'
            }).render();
        }

        Y.Test.Runner.enableLogging();
        Y.Test.Runner.run();
    });
});
</script>
</body>
</html>
